//
//  QJMineShouYiVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/26.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJMineNewShouYiVC.h"
#import "QJMineNewSYHeaderView.h"
#import "QJMineSKJLVC.h"
#import "QJMineSYLSVC.h"
#import "QJMineTXVC.h"
#import "QJMineHistoryFHVC.h"

@interface QJMineNewShouYiVC ()

@property (nonatomic, strong) UITableView *syTableView;
@property (nonatomic, strong) QJMineNewSYHeaderView *syHeaderView;
@property (nonatomic, strong) MineShouYiNewModel *model;
@end

@implementation QJMineNewShouYiVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"我的收益";
    [self setupShouYiVCAction];
    
}

- (void)setupShouYiVCAction{
    
    UserInfoModel *model = [QJUserInfo getCurrentUser];
    NSString *userTye = model.user_type;
    if ([userTye isEqualToString:@"3"]) {
        self.skjlBtn.hidden = NO;
    }else{
        self.skjlBtn.hidden = YES;
    }
    
//    // 测试
//    self.skjlBtn.hidden = NO;
//    // 测试结束
    
    self.syTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - 44) style:UITableViewStylePlain];
    [self.headerView addSubview:self.syTableView];
    self.syHeaderView = [[[NSBundle mainBundle]loadNibNamed:@"QJMineNewSYHeaderView" owner:self options:nil]firstObject];
    self.syHeaderView.frame = CGRectMake(0, 0, UISCREEN_WIDTH, 1400);
    [self.syHeaderView setupHeaderUIAction];
    self.syTableView.tableHeaderView = self.syHeaderView;
    self.syTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(syDataRequestAction)];

    WEAKSELF
    self.syHeaderView.headerBtnClickBlockAction = ^(NSInteger tag) {
        switch (tag) {
            case 300:
            {
                //查看流水
                QJMineSYLSVC *VC = [[QJMineSYLSVC alloc]init];
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 301:
            {
                //去提现
                QJMineTXVC *VC = [[QJMineTXVC alloc]init];
                VC.ktxStr = _model.balance_income;
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 302:
            {
                //历史分红指数
                QJMineHistoryFHVC *VC = [[QJMineHistoryFHVC alloc]init];
                [weakSelf.navigationController pushViewController:VC animated:YES];
            }
                break;
            case 303:
            {
                //如何获取1
                [MBPHUD showAlertWithTitle:@"泉界商家/泉界会员推荐注册的用户，在泉界商城产生消费，即可获得金元宝，产生的收益可提现！" duration:3.0];
            }
                break;
            case 304:
            {
                //如何获取2
                [MBPHUD showAlertWithTitle:@"泉界商家/泉界会员，在泉界商城自己消费，即可获得银元宝，产生的收益可用于消费！" duration:3.0];
            }
                break;
                
            default:
                break;
        }
    };
    
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    [self setNavAlpha:YES];
//    self.navigationController.navigationBarHidden = YES;
    [self syDataRequestAction];
    if (IOS_VERSION < 11.0) {
        self.navTopHeight.constant = 84;
    }else{
        self.navTopHeight.constant = 64;
    }
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self setNavAlpha:NO];
    self.navigationController.navigationBarHidden = NO;
}
#pragma mark -- 数据请求方法
- (void)syDataRequestAction{
    [self.syTableView.mj_header endRefreshing];

    WEAKSELF
    [WWNetRequest fetchMyEarningsWithParameters:nil success:^(id response) {
        NSLog(@"我的收益 response = %@", response);
        if ([response isKindOfClass:[NSDictionary class]]) {
            NSDictionary *newDict = response[@"data"][@"new"];
            NSDictionary *oldDict = response[@"data"][@"old"];
            NSDictionary *totalDict = response[@"data"][@"total"];
            NSDictionary *indexDict = response[@"data"][@"index"];
            NSString *is_old =  [NSString stringWithFormat:@"%@",response[@"data"][@"is_old"]];
                              
//            if ([is_old isEqualToString:@"1"]) {
            
                MineShouYiNewModel *model = [[MineShouYiNewModel alloc]init];
                
                model.total_business = [NSString stringWithFormat:@"%.2f",[newDict[@"total_business"]floatValue]];//总收益
                model.frozen_business = [NSString stringWithFormat:@"%.2f",[newDict[@"frozen_business"]floatValue]];//冻结
                model.raise_business = [NSString stringWithFormat:@"%.2f",[newDict[@"raise_business"]floatValue]];//已提现
                model.balance_income = [NSString stringWithFormat:@"%.2f",[newDict[@"balance_income"]floatValue]];//可提现金额
                model.yesterday_dner = [NSString stringWithFormat:@"%.4f",[indexDict[@"offline"]floatValue]];//昨日第纳尔分红指数
                model.yesterday_yb = [NSString stringWithFormat:@"%.4f",[indexDict[@"offline"]floatValue]];//昨日元宝分红指数
            
            if ([is_old isEqualToString:@"1"]) {
                
                 model.yesterday_yb = [NSString stringWithFormat:@"%.4f",[indexDict[@"old"]floatValue]];//昨日元宝分红指数
            }
                
                model.balance_gold = [NSString stringWithFormat:@"%.2f",[newDict[@"balance_gold"]floatValue]];//可用金第纳尔
                model.frozen_gold = [NSString stringWithFormat:@"%.2f",[newDict[@"frozen_gold"]floatValue]];//待发金第纳尔
                model.raise_gold = [NSString stringWithFormat:@"%.2f",[newDict[@"raise_gold"]floatValue]];//已扣除金第纳尔
                model.yesterday_income = [NSString stringWithFormat:@"%.2f",[newDict[@"yesterday_income"]floatValue]];//昨日金第纳尔收益
                
                
                model.gold_ky = [NSString stringWithFormat:@"%.2f",[oldDict[@"gold_ky"]floatValue]];//金元宝可用
                model.gold_df = [NSString stringWithFormat:@"%.2f",[oldDict[@"gold_df"]floatValue]];//金元宝待发
                model.gold_yk = [NSString stringWithFormat:@"%.2f",[oldDict[@"gold_ykc"]floatValue]];//金元宝已扣除
                model.gold_sy = [NSString stringWithFormat:@"%.2f",[oldDict[@"gold_sy"]floatValue]];//昨日金元宝收益
                
                
                 model.balance_silver = [NSString stringWithFormat:@"%.2f",[newDict[@"balance_silver"]floatValue]];//可用银第纳尔
                 model.raise_silver = [NSString stringWithFormat:@"%.2f",[newDict[@"raise_silver"]floatValue]];//已消费银第纳尔
                 model.yesterday_silver = [NSString stringWithFormat:@"%.2f",[newDict[@"yesterday_silver"]floatValue]];//昨日银第纳尔收益
                
               
                 model.silver_ky = [NSString stringWithFormat:@"%.2f",[oldDict[@"silver_ky"]floatValue]];//银元宝可用
                 model.silver_yxf = [NSString stringWithFormat:@"%.2f",[oldDict[@"silver_yxf"]floatValue]];//银元宝已消费
                 model.silver_zr_sy = [NSString stringWithFormat:@"%.2f",[oldDict[@"silver_zr_sy"]floatValue]];//昨日银元宝收益
                 model.silver_total_sy = [NSString stringWithFormat:@"%.2f",[oldDict[@"silver_total_sy"]floatValue]];//银元宝总收益
                 model.balance_integral = [NSString stringWithFormat:@"%.2f",[newDict[@"balance_integral"]floatValue]];//银元宝剩余积分
                weakSelf.model = model;
                [weakSelf.syHeaderView headerUpdateWithModel:model];
                
//            }
        }
    } failure:^(NSDictionary *error) {
        NSLog(@"我的收益 error = %@", error);

    }];
}
- (IBAction)backBtnClickAction:(UIButton *)sender {
    
    KPopViewControllerAction;
}
//收款记录
- (IBAction)skBtnClockAction:(UIButton *)sender {
    QJMineSKJLVC *VC = [[QJMineSKJLVC alloc]init];
    KPushViewControllerAction(VC);
}



@end
